Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
How to look for an item in DB when the type is String | String[] using typescript union types

I am calling a method sendEmail that sends an email to a single user or a bunch of users(the type of to email is string | string[])

Is there a cleaner/better way to determine if it's an array or a single user so that I can look for it in the DB accordingly? The code looks quite repetitive now.

  public async sendEmail(
    toEmail: string | string[],
  ): Promise<void> {
    if (!Array.isArray(toEmail)) {
      const user = await this.userRepository.getUserByEmail(toEmail);
      await checkIfPaymentMade(user);
    } else {
      for (const email of toEmail) {
       const user = await this.userRepository.getUserByEmail(toEmail);
        await checkIfPaymentMade(user);
      }
    }
  }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

A few other options come to mind

  1. Extract the common code to its own function. There's still some duplication, but just one line instead of however long the helper function needs to be.
public async sendEmail(
  toEmail: string | string[]
): Promise<void> {
  if (!Array.isArray(toEmail)) {
    await doStuff(toEmail);
  } else {
    for (const email of toEmail) {
      await doStuff(email);
    }
  }
}

private async doStuff(toEmail: string): Promise<void> {
  const user = await this.userRepository.getUserByEmail(toEmail);
  await checkIfPaymentMade(user);
}
  1. Have the body of the function only work with standalone strings. If it's an array, recurse.
public async sendEmail(
  toEmail: string | string[],
): Promise<void> {
  if (Array.isArray(toEmail) {
    for (const email of toEmail) {
      await sendEmail(email)
    }
  } else {
    const user = await this.userRepository.getUserByEmail(email);
    await checkIfPaymentMade(user);
  }
}
  1. Have the body of the function only work with arrays. If it's not an array, turn it into one first.
public async sendEmail(
  toEmail: string | string[],
): Promise<void> {
  const emailArray = Array.isArray(toEmail) ? toEmail : [toEmail];

  for (const email of emailArray) {
    const user = await this.userRepository.getUserByEmail(email);
    await checkIfPaymentMade(user);
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If you just want to keep it DRY, here's an option:

TS Playground link

public async sendEmail (recipients: string | string[]): Promise<void> {
  for (const address of Array.isArray(recipients) ? recipients : [recipients]) {
    const user = await this.userRepository.getUserByEmail(address);
    await checkIfPaymentMade(user);
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda